<template xmlns:el-row="http://www.w3.org/1999/html">
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" >
      <el-row>
        <el-col :span="6">
          <el-form-item label="学生名称" prop="stuName" style="width: 100%;">
            <el-input
              style="width: 100%;"
              v-model="queryParams.stuName"
              placeholder="请输入学生名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="学生性别" prop="stuSex" style="width: 100%;">
            <el-select v-model="queryParams.stuSex" placeholder="请选择学生性别" clearable size="small" style="width: 100%;">
              <el-option
                v-for="item in sysUserSex"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
                @keyup.enter.native="handleQuery"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="学生年龄" prop="stuAge" style="width: 100%;">
            <el-input
              v-model="queryParams.stuAge"
              placeholder="请输入学生年龄"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="手机号码" prop="stuPhone" style="width: 100%;">
            <el-input
              style="width: 100%;"
              v-model="queryParams.stuPhone"
              placeholder="请输入手机号码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="6">
          <el-form-item label="在校状态" prop="stuState" style="width: 100%;">
            <el-select v-model="queryParams.stuState" placeholder="在校状态" clearable size="small">
              <el-option
                v-for="item in stuStateList"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
                @keyup.enter.native="handleQuery">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="报名时间" prop="stuRegistrationtime" style="width: 100%;">
            <el-date-picker
              clearable size="small"
              v-model="queryParams.stuRegistrationtime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择报名时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="班级id" prop="stuClassId" style="width: 100%;">
            <el-input
              style="width: 100%;"
              v-model="queryParams.stuClassId"
              placeholder="请输入班级id"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['manage:student:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:student:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:student:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['manage:student:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align="center" />
<!--      <el-table-column label="学生id" align="center" prop="id" />-->
      <el-table-column label="班级id" align="center" prop="stuClassId" />
      <el-table-column label="学生名称" align="center" prop="stuName" />
      <el-table-column label="学生性别" align="center" prop="stuSex" :formatter="statusFormatterSex"/>
      <el-table-column label="年龄" align="center" prop="stuAge" width="55"/>
      <el-table-column label="手机号码" align="center" prop="stuPhone" />
      <el-table-column label="出生日期" align="center" prop="stuBirthday" />
      <el-table-column label="省份证号" align="center" prop="stuId" />
      <el-table-column label="毕业学校" align="center" prop="stuGraduateschool" />
      <el-table-column label="最高学历" align="center" prop="stuEducation" />
      <el-table-column label="在校状态" align="center" prop="stuState" :formatter="statusFormatter"/>
<!--      <el-table-column label="学生民族" align="center" prop="stuNation" />-->
      <el-table-column label="报名时间" align="center" prop="stuRegistrationtime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stuRegistrationtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:student:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:student:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生名称" prop="stuName">
          <el-input v-model="form.stuName" placeholder="请输入学生名称" />
        </el-form-item>
        <el-form-item label="学生性别" prop="stuSex">
          <el-select v-model="form.stuSex" placeholder="请选择学生性别">
            <el-option
              v-for="item in sysUserSex"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
              @keyup.enter.native="handleQuery"/>
          </el-select>
        </el-form-item>
        <el-form-item label="学生年龄" prop="stuAge">
          <el-input v-model="form.stuAge" placeholder="请输入学生年龄" />
        </el-form-item>
        <el-form-item label="手机号码" prop="stuPhone">
          <el-input v-model="form.stuPhone" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="学生出生日期" prop="stuBirthday">
          <el-date-picker
            v-model="form.stuBirthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="省份证号码" prop="stuId">
          <el-input v-model="form.stuId" placeholder="请输入省份证号码" />
        </el-form-item>
        <el-form-item label="毕业学校" prop="stuGraduateschool">
          <el-input v-model="form.stuGraduateschool" placeholder="请输入毕业学校" />
        </el-form-item>
        <el-form-item label="最高学历" prop="stuEducation">
          <el-input v-model="form.stuEducation" placeholder="请输入最高学历" />
        </el-form-item>
        <el-form-item label="学生在校状态" prop="stuState">
          <el-select v-model="queryParams.stuState" placeholder="在校状态" clearable size="small">
            <el-option
              v-for="item in stuStateList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
              @keyup.enter.native="handleQuery">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学生民族" prop="stuNation">
          <el-input v-model="form.stuNation" placeholder="请输入学生民族" />
        </el-form-item>
        <el-form-item label="报名时间" prop="stuRegistrationtime">
          <el-date-picker clearable size="small"
            v-model="form.stuRegistrationtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择报名时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="班级id" prop="stuClassId">
          <el-input v-model="form.stuClassId" placeholder="请输入班级id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStudent, getStudent, delStudent, addStudent, updateStudent, exportStudent } from "@/api/manage/student";

export default {
  name: "Student",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生表格数据
      studentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stuName: null,
        stuSex: null,
        stuAge: null,
        stuPhone: null,
        stuBirthday: null,
        stuId: null,
        stuGraduateschool: null,
        stuEducation: null,
        stuState: null,
        stuNation: null,
        stuRegistrationtime: null,
        stuClassId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 在校状态
      stuStateList:[],
      // 学生性别(字典)
      sysUserSex:[],
    };
  },
  created() {
    this.getList();
    this.getDicts("stu_state_list").then(response => {
      this.stuStateList = response.data;
    });
    this.getDicts("sys_user_sex").then(response => {
      this.sysUserSex = response.data;
    });
  },
  methods: {
    /** 查询学生列表 */
    getList() {
      this.loading = true;
      listStudent(this.queryParams).then(response => {
        this.studentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stuName: null,
        stuSex: null,
        stuAge: null,
        stuPhone: null,
        stuBirthday: null,
        stuId: null,
        stuGraduateschool: null,
        stuEducation: null,
        stuState: null,
        stuNation: null,
        stuRegistrationtime: null,
        stuClassId: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStudent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStudent(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStudent(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除学生编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delStudent(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有学生数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportStudent(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },

    statusFormatter(value){
      return this.selectDictLabel(this.stuStateList,value.stuState);
    },
    statusFormatterSex(value){
      console.log(JSON.stringify(value))
      return this.selectDictLabel(this.sysUserSex,value.stuSex);
    }
  }
};
</script>
